package com.gujiangbo.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/3/11 08:55
 * @Version: 1.0
 * @Description:
 */
public class ExecutorPoolDemo {

    public static void main(String[] args) {
        //newFixedThreadPoolTest();
        //newSingleThreadExecutorTest();
        newCachedThreadExecutorTest();
    }


    /**
     * 定长线程池
     */
    public static void newFixedThreadPoolTest() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        try {
            IntStream.range(0, 10).forEach(v -> executorService.execute(() -> System.out.println(Thread.currentThread().getName() + "\t" + "办理业务")));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

    }


    /**
     * 单例线程池
     */
    public static void newSingleThreadExecutorTest() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
            IntStream.range(0, 10).forEach(v -> executorService.execute(() -> System.out.println(Thread.currentThread().getName() + "\t" + "办理业务")));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    /**
     * 最大线程池，整型最大值量
     */
    public static void newCachedThreadExecutorTest() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            IntStream.range(0, 10).forEach(v -> executorService.execute(() -> System.out.println(Thread.currentThread().getName() + "\t" + "办理业务")));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}
